package com.coolweather.android;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.coolweather.android.service.MD5;
import com.example.coolweather.R;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText UserName;
    private EditText Password;
    private Button login;
    private Button register;
    String username,password,MD5password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_login);

        initView();
    }

    private void initView() {
        UserName=(EditText)findViewById(R.id.username);
        Password=(EditText)findViewById(R.id.password);
        login=(Button)findViewById(R.id.btn_login);
        register=(Button)findViewById(R.id.btn_regist);

        login.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.btn_regist:
               /*
                * 登录按钮左下角有注册字样，没有账号的可以跳转到注册界面
                */
               startActivity(new Intent(this,RegisterActivity.class));
               break;
           
           case R.id.btn_login:
               login();
               // Toast.makeText(this,"登录成功",Toast.LENGTH_LONG).show();
               //startActivity(new Intent(this,MainActivity.class));
               break;

           default:
               break;
       }
    }

    private void login() {
        //开始登录，获取用户名和密码 getText().toString().trim();
        username=UserName.getText().toString().trim();
        password=Password.getText().toString().trim();
        String MD5pwd= MD5.md5Password(password);
        MD5password=readPwd(username);
        if(TextUtils.isEmpty(username)){
            Toast.makeText(LoginActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
        }else if(TextUtils.isEmpty(password)){
            Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            // 判断，输入的密码加密后，是否与保存在SharedPreferences中一致
        }else if(MD5password.equals(MD5pwd)){
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

            //保存登录状态，在界面保存登录的用户名 定义个方法 saveLoginStatus boolean 状态 , userName 用户名;
            saveLoginStatus(true, username);

            //登录成功后关闭此页面进入主页
            Intent data = new Intent();
            data.putExtra("isLogin", true);
            //RESULT_OK为Activity系统常量，状态码为-1
            // 表示此页面下的内容操作成功将data返回到上一页面，如果是用back返回过去的则不存在用setResult传递data值
            setResult(RESULT_OK, data);
            //销毁登录界面
            LoginActivity.this.finish();
            startActivity(new Intent(LoginActivity.this, WeatherActivity.class));
        }else if(MD5pwd!=null&&!TextUtils.isEmpty(MD5pwd)&&!MD5password.equals(MD5pwd)){
            Toast.makeText(LoginActivity.this, "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(LoginActivity.this, "此用户名不存在", Toast.LENGTH_SHORT).show();
        }
    }

    private String readPwd(String username){
        //"loginInfo",mode_private; MODE_PRIVATE表示可以继续写入
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        return sp.getString(username , "");
    }
    private void saveLoginStatus(boolean status,String userName){
        //loginInfo表示文件名  SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        //获取编辑器
        SharedPreferences.Editor editor=sp.edit();
        //存入boolean类型的登录状态
        editor.putBoolean("isLogin", status);
        //存入登录状态时的用户名
        editor.putString("loginUserName", userName);
        //提交修改
        editor.apply();
    }
}